package com.qyhl.module_practice.rank;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeListBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeRankBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeTeamBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PracticeRankFragment extends BaseFragment implements PracticeRankContract.PracticeRankView {
    @BindView(R2.id.street_type)
    TextView streetType;
    @BindView(R2.id.street_recyclerView)
    RecyclerView streetRecyclerView;
    @BindView(R2.id.team_type)
    TextView teamType;
    @BindView(R2.id.team_recyclerView)
    RecyclerView teamRecyclerView;
    @BindView(R2.id.person_type)
    TextView personType;
    @BindView(R2.id.person_recyclerView)
    RecyclerView personRecyclerView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    private PracticeRankPresenter mPresenter;

    private String instId;

    private int volId;

    private int type;

    public void setInstId(String instId) {
        this.instId = instId;
    }

    public void setVolId(int volId) {
        this.volId = volId;
    }

    public void setType(int type) {
        this.type = type;
    }

    private CommonAdapter<PracticeListBean> streetAdapter;

    private List<PracticeListBean> streetList = new ArrayList<>();

    private CommonAdapter<PracticeTeamBean> teamAdapter;

    private List<PracticeTeamBean> teamList = new ArrayList<>();

    private CommonAdapter<PracticeVolunteerBean> personAdapter;

    private List<PracticeVolunteerBean> personList = new ArrayList<>();

    public static PracticeRankFragment newInstance(String instId, int volId, int type) {
        PracticeRankFragment fragment = new PracticeRankFragment();
        fragment.setInstId(instId);
        fragment.setVolId(volId);
        fragment.setType(type);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.practice_fragment_rank, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeRankPresenter(this);
        loadMask.setStatus(LoadingLayout.Loading);
        refresh.setEnableLoadMore(false);
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setRefreshHeader(new MaterialHeader(getContext()));

        switch (type) {
            case 1:
                streetType.setText("实践单位服务时长排行（小时）");
                teamType.setText("团体服务时长排行（小时）");
                personType.setText("志愿者服务时长排行（小时）");
                break;
            case 2:
                streetType.setText("实践单位服务项目排行（个）");
                teamType.setText("团体服务项目排行（个）");
                personType.setText("志愿者服务项目排行（个）");
                break;
            case 3:
                streetType.setText("实践单位爱心积分排行");
                teamType.setText("团体爱心积分排行");
                personType.setText("志愿者爱心积分排行");
                break;
        }

        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.practice_item_news_divider));

        streetRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        streetRecyclerView.addItemDecoration(divider);
        streetRecyclerView.setAdapter(streetAdapter = new CommonAdapter<PracticeListBean>(getContext(), R.layout.practice_item_rank_street, streetList) {
            @Override
            protected void convert(ViewHolder holder, PracticeListBean bean, int position) {
                holder.setText(R.id.title, bean.getName());

                ImageView cover = holder.getView(R.id.cover);

                Glide.with(PracticeRankFragment.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(cover);

                switch (type) {
                    case 1:
                        holder.setText(R.id.value_num, DateUtils.PracticeServiceTimeFormat(bean.getActTimes()));
                        break;
                    case 2:
                        holder.setText(R.id.value_num, bean.getActNums() + "");
                        break;
                    case 3:
                        holder.setText(R.id.value_num, bean.getScore() + "");
                        break;
                }

                switch (position) {
                    case 0:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no1_icon);

                        break;
                    case 1:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no2_icon);

                        break;
                    case 2:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no3_icon);

                        break;
                    default:
                        holder.setVisible(R.id.rank_num, true);
                        holder.setVisible(R.id.rank_num_tag, false);

                        holder.setText(R.id.rank_num, "No." + (position + 1));
                        break;
                }
            }
        });

        teamRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        teamRecyclerView.addItemDecoration(divider);
        teamRecyclerView.setAdapter(teamAdapter = new CommonAdapter<PracticeTeamBean>(getContext(), R.layout.practice_item_rank_team, teamList) {
            @Override
            protected void convert(ViewHolder holder, PracticeTeamBean bean, int position) {
                holder.setText(R.id.title, bean.getName());

                switch (type) {
                    case 1:
                        holder.setText(R.id.value_num, DateUtils.PracticeServiceTimeFormat(bean.getActTimes()));
                        break;
                    case 2:
                        holder.setText(R.id.value_num, bean.getActNums() + "");
                        break;
                    case 3:
                        holder.setText(R.id.value_num, bean.getActScore() + "");
                        break;
                }

                switch (position) {
                    case 0:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no1_icon);

                        break;
                    case 1:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no2_icon);

                        break;
                    case 2:
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no3_icon);

                        break;
                    default:
                        holder.setVisible(R.id.rank_num, true);
                        holder.setVisible(R.id.rank_num_tag, false);

                        holder.setText(R.id.rank_num, "No." + (position + 1));
                        break;
                }
            }
        });

        personRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        personRecyclerView.addItemDecoration(divider);

        personRecyclerView.setAdapter(personAdapter = new CommonAdapter<PracticeVolunteerBean>(getContext(), R.layout.practice_item_rank, personList) {
            @Override
            protected void convert(ViewHolder holder, PracticeVolunteerBean bean, int position) {
                holder.setText(R.id.name, bean.getName());

                switch (type) {
                    case 1:
                        holder.setText(R.id.value_num, DateUtils.PracticeServiceTimeFormat(bean.getServiceTimes()));
                        break;
                    case 2:
                        holder.setText(R.id.value_num, bean.getServiceNums() + "");
                        break;
                    case 3:
                        holder.setText(R.id.value_num, bean.getTotalScore() + "");
                        break;
                }

                RoundedImageView head = holder.getView(R.id.head_icon);
                Glide.with(PracticeRankFragment.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                        .into(head);

                switch (position) {
                    case 0:
                        holder.setVisible(R.id.head_tag, true);
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no1_icon);
                        holder.setImageResource(R.id.head_tag, R.drawable.practice_rank_head_one_icon);
                        if (bean.getId() == volId) {
                            holder.setTextColorRes(R.id.value_num, R.color.global_base);
                            holder.setVisible(R.id.myself_tag, true);
                            holder.setTextColorRes(R.id.name, R.color.global_base);
                        } else {
                            holder.setTextColorRes(R.id.value_num, R.color.global_black_lv1);
                            holder.setVisible(R.id.myself_tag, false);
                            holder.setTextColorRes(R.id.name, R.color.global_black_lv1);
                        }
                        break;
                    case 1:
                        holder.setVisible(R.id.head_tag, true);
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no2_icon);
                        holder.setImageResource(R.id.head_tag, R.drawable.practice_rank_head_two_icon);
                        if (bean.getId() == volId) {
                            holder.setTextColorRes(R.id.value_num, R.color.global_base);
                            holder.setVisible(R.id.myself_tag, true);
                            holder.setTextColorRes(R.id.name, R.color.global_base);
                        } else {
                            holder.setTextColorRes(R.id.value_num, R.color.global_black_lv1);
                            holder.setVisible(R.id.myself_tag, false);
                            holder.setTextColorRes(R.id.name, R.color.global_black_lv1);
                        }
                        break;
                    case 2:
                        holder.setVisible(R.id.head_tag, true);
                        holder.setVisible(R.id.rank_num, false);
                        holder.setVisible(R.id.rank_num_tag, true);

                        holder.setImageResource(R.id.rank_num_tag, R.drawable.practice_rank_no3_icon);
                        holder.setImageResource(R.id.head_tag, R.drawable.practice_rank_head_three_icon);
                        if (bean.getId() == volId) {
                            holder.setTextColorRes(R.id.value_num, R.color.global_base);
                            holder.setVisible(R.id.myself_tag, true);
                            holder.setTextColorRes(R.id.name, R.color.global_base);
                        } else {
                            holder.setTextColorRes(R.id.value_num, R.color.global_black_lv1);
                            holder.setVisible(R.id.myself_tag, false);
                            holder.setTextColorRes(R.id.name, R.color.global_black_lv1);
                        }
                        break;
                    default:
                        holder.setVisible(R.id.head_tag, false);
                        holder.setVisible(R.id.rank_num, true);
                        holder.setVisible(R.id.rank_num_tag, false);

                        if (bean.getId() == volId) {
                            holder.setTextColorRes(R.id.value_num, R.color.global_base);
                            holder.setVisible(R.id.myself_tag, true);
                            holder.setTextColorRes(R.id.name, R.color.global_base);
                            holder.setTextColorRes(R.id.rank_num, R.color.global_base);
                        } else {
                            holder.setTextColorRes(R.id.value_num, R.color.global_black_lv1);
                            holder.setVisible(R.id.myself_tag, false);
                            holder.setTextColorRes(R.id.name, R.color.global_black_lv1);
                            holder.setTextColorRes(R.id.rank_num, R.color.global_black_lv1);
                        }

                        holder.setText(R.id.rank_num, "No." + (position + 1));
                        break;
                }
            }
        });


        mPresenter.getData(instId, type);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getData(instId, type);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getData(instId, type);
            }
        });

        streetAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("instId", instId);
                bundle.putString("streetId", streetList.get(position).getId() + "");
                bundle.putString("title", streetList.get(position).getName());
                bundle.putString("volId", volId+"");
                RouterManager.router(ARouterPathConstant.PRACTICE_STREET_DETAIL, bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        teamAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("orgId", teamList.get(position).getId() + "");
                bundle.putString("volId", volId + "");
                bundle.putString("instId", instId);
                RouterManager.router(ARouterPathConstant.PRACTICE_TEAM_DETAIL, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void initImmersionBar() {

    }


    @OnClick({R2.id.street_more, R2.id.team_more, R2.id.person_more})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.street_more) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", type);
            bundle.putString("instId", instId);
            RouterManager.router(ARouterPathConstant.PRACTICE_RANK_INST, bundle);

        } else if (id == R.id.team_more) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", type);
            bundle.putString("instId", instId);
            RouterManager.router(ARouterPathConstant.PRACTICE_RANK_TEAM, bundle);
        } else if (id == R.id.person_more) {
            Bundle bundle = new Bundle();
            bundle.putInt("id", volId);
            bundle.putInt("type", type);
            bundle.putString("instId",instId);
            RouterManager.router(ARouterPathConstant.PRACTICE_SCORE_RANK, bundle);
        }
    }

    @Override
    public void setData(PracticeRankBean bean) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        personList.clear();
        teamList.clear();
        streetList.clear();

        personList.addAll(bean.getVolList());
        teamList.addAll(bean.getOrgList());
        streetList.addAll(bean.getInsList());

        personAdapter.notifyDataSetChanged();
        teamAdapter.notifyDataSetChanged();
        streetAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(getContext())) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }
}
